///////////////////////////////////////////////////////////////////////////////
// Name:        wxAutoTextCtrl.cpp
// Purpose:     wxAutoTextCtrl class implementation.
// Author:      Priyank Bolia <soft@priyank.in>
// Modified by: Ville Lindell <vdell@users.sf.net>
// Created:     25.01.06
// Copyright:   (c) Priyank Bolia
// Licence:     wxWindows licence <http://wxwidgets.org/licence3.txt>
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.hpp"
#include "wxAutoTextCtrl.hpp"

IMPLEMENT_DYNAMIC_CLASS ( wxAutoTextCtrl, wxTextCtrl )

wxAutoTextCtrl::wxAutoTextCtrl() : mKeyCode(0), mArrayIndex(-1) 
{
	Connect ( -1, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler
		( wxAutoTextCtrl::OnTextChange ) );
	Connect ( -1, wxEVT_CHAR, wxKeyEventHandler ( wxAutoTextCtrl::OnChar ) );
	Connect ( -1, wxEVT_KEY_DOWN, wxKeyEventHandler ( wxAutoTextCtrl::OnKeyDown ) );
}

wxAutoTextCtrl::wxAutoTextCtrl ( wxWindow* parent, wxWindowID id, 
								const wxString& value, const wxPoint& pos, 
								const wxSize& size, long style, 
								const wxValidator& validator, const wxString& name )
								: wxTextCtrl ( parent, id, value, pos, size, style, 
								validator, name ), mKeyCode(0), 
								mArrayIndex(-1) 
{
	Connect ( id, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler
		( wxAutoTextCtrl::OnTextChange ) );
	Connect ( id, wxEVT_CHAR, wxKeyEventHandler ( wxAutoTextCtrl::OnChar ) );
	Connect ( id, wxEVT_KEY_DOWN, wxKeyEventHandler ( wxAutoTextCtrl::OnKeyDown ) );
}

void wxAutoTextCtrl::OnTextChange ( wxCommandEvent &rEvent )
{
	if ( IsAutoCompleteEnabled() )
	{
		if ( mKeyCode != WXK_BACK && mKeyCode != WXK_DELETE ) 
		{
			const wxString strValue = GetValue();
			wxString strArray;
			for ( wxUint32 i = 0; i < mStrValues.GetCount(); i++ )
			{
				strArray = mStrValues[i];
				if ( strArray.Find ( strValue ) == 0 )
				{
					if ( strArray.length() > strValue.length() )
					{
						mArrayIndex = i;
						SetValue ( strArray );
						SetSelection ( ( long ) strValue.length(), -1 );
					}
					rEvent.Skip();
					return;
				}
			}
		}
	}
	rEvent.Skip();
}

void wxAutoTextCtrl::OnChar ( wxKeyEvent &rEvent )
{
	if ( IsAutoCompleteEnabled() ) { mKeyCode = rEvent.GetKeyCode(); }
	rEvent.Skip();
}

void wxAutoTextCtrl::OnKeyDown ( wxKeyEvent &rEvent )
{
	if ( IsAutoCompleteEnabled() )
	{
		mKeyCode = 0;
		const wxInt32 nKeyCode = rEvent.GetKeyCode();
		if ( nKeyCode == WXK_UP || nKeyCode == WXK_DOWN )
		{
			wxString strValue = GetValue();
			wxString strArray;
			long start = -1, end = -1;
			GetSelection ( &start, &end );
			if ( start >= 0 )
			{
				strValue = strValue.Left ( start );
			}
			if ( nKeyCode == WXK_DOWN ) 
			{
				for( wxUint32 i = ( mArrayIndex + 1 ); i < mStrValues.GetCount() 
					&& mArrayIndex >= 0; i++ )
				{
					strArray = mStrValues[i];
					if ( strArray.Find ( strValue ) == 0 )
					{
						mArrayIndex = i;
						SetValue ( strArray );
						SetSelection ( ( long ) strValue.length(), -1 );
						return;
					}
				}
			}
			else
			{
				for( wxUint32 i = ( mArrayIndex - 1 ); i >= 0 && 
					mArrayIndex > 0 && i < mStrValues.GetCount(); i-- )
				{
					strArray = mStrValues[i];
					if ( strArray.Find ( strValue ) == 0 )
					{
						mArrayIndex = i;
						SetValue ( strArray );
						SetSelection ( ( long ) strValue.length(), -1 );
						return;
					}
				}
			}
			return;
		}
		mArrayIndex = -1;
	}
	rEvent.Skip ();
}